import { reactive, markRaw } from 'vue'
import { removeUndefinedParams } from '@/utils/index'
import type { CTableProps } from '@/components/CTable/index'
import type { BasicPagingListParams } from '@/apis/basicModel'
import type { IFlowListItem, IFlowParams } from '@/apis/pcf/myModel/model/modelDetailModel'
import { getFlowList } from '@/apis/pcf/myModel/modelDetail'
import type { SearchOptionsType, TableFieldsSettingType } from '@/components/CPageTable'
import { ColumnSettingTypeEnum } from '@/components/CPageTable'
export const useFactorList = (modelNo: string) => {
  // 搜索字段及其他
  const searchForm = reactive<IFlowParams>({
    modelNo: '',
    dataType: undefined,
    lifeCycleStage: undefined,
    nodeName: undefined,
    relateStatus: undefined
  })

  // 搜索配置
  const searchOptions: SearchOptionsType[] = markRaw([
    {
      name: '关联状态',
      field: 'relateStatus',
      type: 'radio',
      fixedOption: 'AllRelateStatueOptions'
    },
    {
      name: '流名称',
      field: 'nodeName',
      type: 'input'
    },
    {
      name: '流类型',
      field: 'dataType',
      type: 'select',
      fixedOption: 'FlowTypeOptions'
    },
    {
      name: '所属阶段',
      field: 'lifeCycleStage',
      type: 'select',
      fixedOption: 'ProcessStageOption',
      style: 'width: 160px'
    }
  ])

  // 表格配置
  const tableOptions: CTableProps = {
    loadData,
    selection: true,
    hasCollapse: false
  }

  const tableFieldsSetting: TableFieldsSettingType[] = [
    {
      title: '流类型',
      field: 'dataType',
      width: 100,
      slot: 'type',
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '所属阶段',
      field: 'lifeCycleStage',
      width: 150,
      slot: 'stage',
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '流名称',
      field: 'nodeName',
      slot: 'nodeNameSlot',
      minWidth: 190,
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '所属层级',
      field: 'nodePath',
      minWidth: 140,
      slot: 'level',
      columnSettingConfig: {
        enabled: true,
        type: ColumnSettingTypeEnum.REQUIRED
      }
    },
    {
      title: '关键属性',
      field: 'keyProperty',
      width: 200,
      slot: 'keyProperty',
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '活动数据',
      field: 'dosage',
      minWidth: 150,
      slot: 'data',
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '因子数值',
      field: 'factorNo',
      minWidth: 200,
      slot: 'factorValue',
      columnSettingConfig: {
        enabled: true,
        type: ColumnSettingTypeEnum.REQUIRED
      }
    },
    {
      title: '操作',
      field: 'op',
      slot: 'op',
      minWidth: 130,
      fixed: 'right',
      columnSettingConfig: {
        enabled: true,
        type: ColumnSettingTypeEnum.FIXED
      }
    }
  ]

  function loadData(parameter: BasicPagingListParams) {
    const params = handleParams()
    Object.assign(params, parameter)
    return getFlowList(params).then((res) => {
      res.records.forEach((item) => {
        if (item.keyProperty && item.keyProperty.includes('\r')) {
          item.keyProperty = item.keyProperty.replaceAll('\r', '<br/>')
        }
      })
      return {
        records: res.records,
        total: res.total,
        pages: res.pages
      }
    })
  }
  const handleParams = (): IFlowParams => {
    let params: IFlowParams = {
      modelNo,
      dataType: searchForm.dataType,
      lifeCycleStage: searchForm.lifeCycleStage,
      nodeName: searchForm.nodeName,
      relateStatus: searchForm.relateStatus
    }
    params = removeUndefinedParams(params)
    return params
  }

  return {
    // 头部右侧筛选项配置
    searchOptions,
    // 筛选项字段
    searchForm,
    // 表格配置
    tableOptions,
    tableFieldsSetting
  }
}
